const app = getApp()
Page({
  data: {
    picPath: app.globalData.picPath,
    canvas: {},
    penSigners: '',
    flag: '',
    status: true
  },

  editData() {
    this.setData({
      status: false
    })
    this.canHandle()
  },
  // 获取签名版
  getPenSigner() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + 'member/getPenSigner',
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId
    }, res => {
      if (res.flag) {
        console.log(res);
        if (res.data.penSigners.length > 0) {
          that.setData({
            penSigners: res.data.penSigners[0],
            flag: 'edit',
            status: true
          })
        } else {
          that.setData({
            flag: 'add',
            status: false
          })
          that.canHandle()
        }
        wx.hideLoading()
      }
    })
  },
  onLoad() {
    this.getPenSigner()
  },
  onShow() {},

  canHandle() {
    const query = wx.createSelectorQuery()
    query.select('#myCanvas')
      .fields({
        node: true,
        size: true
      })
      .exec((res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')
        const dpr = wx.getSystemInfoSync().pixelRatio
        canvas.width = res[0].width * dpr
        canvas.height = res[0].height * dpr
        this.width = canvas.width;
        this.height = canvas.height;
        ctx.scale(dpr, dpr)
        this.ctx = ctx;
        this.setData({
          canvas: canvas
        })
      })
  },
  scaleStart(e) {
    let {
      x,
      y
    } = e.touches[0]
    console.log(x, y)
    this.ctx.beginPath()
    this.ctx.moveTo(x, y)
    this.ctx.strokeStyle = "#000";
    this.ctx.lineWidth = '5'
  },
  scaleMove(e) {
    let {
      x,
      y
    } = e.touches[0]
    this.ctx.lineTo(x, y)
    this.ctx.stroke();
    this.ctx.lineCap = "round";
    this.ctx.moveTo(x, y)
  },
  scaleEnd(e) {
    console.log(111, e)
    this.ctx.lineCap = "round";
  },
  xiaZai() {
    wx.canvasToTempFilePath({
      canvas: this.data.canvas, // canvas 实例
      success(res) {
        console.log(res);
        // canvas 生成图片成功
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            // 保存成功
            wx.showToast({
              title: '已保存相册',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  clearCanvas() {
    this.ctx.clearRect(0, 0, this.width, this.height)
    this.ctx.draw(true)
  },

  // 上传
  uploadHandle() {
    var penSigners = this.data.penSigners,
      flag = this.data.flag;
    wx.canvasToTempFilePath({
      canvas: this.data.canvas,
      success(res) {
        console.log(res);
        console.log(penSigners);
        console.log(flag);
        console.log(penSigners.penSignerImg);
        // return
        wx.showLoading({
          title: '上传中',
        })

        var url = app.globalData.url + "member/addPenSigner",
          data = {};
        if (flag == 'add') {
          data = {
            flag: flag,
            memberId: app.globalData.memberId,
          }
        } else(
          data = {
            flag: flag,
            penSignerId: penSigners.penSignerId,
            memberId: app.globalData.memberId,
            penSignerImg: penSigners.penSignerImg
          }
        )
        console.log(data);
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePath,
          formData: data,
          name: 'file',
          success(res) {
            var res = JSON.parse(res.data)
            console.log(res);
            wx.hideLoading()
            if (res.flag) {
              wx.showModal({
                title: '提示',
                content: '上传成功',
                showCancel: false,
                complete: (res) => {
                  if (res.confirm) {
                    // wx.switchTab({
                    //   url: '/pages/geren/geren',
                    // })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
})